#include<bits/stdc++.h>
#define int long long 

using namespace std;

signed main() {
	int t;
	cin >> t;
	while(t -- ) {
		int n, m;
		cin >> n >> m;
		int a[n + 10][m + 10];
		for(int i = 1; i <= n; i ++ ) {
			for(int j = 1; j <= m; j ++ ) {
				cin >> a[i][j];
			}
		}
		int dp[n + 10][m + 10];
		memset(dp, 0, sizeof dp);
		dp[1][1] = 1;
		
		for(int i = 1; i <= n; i ++ ) {
			for(int j = 1; j <= m; j ++ ) {
				if(a[i - 1][j] == a[i][j]) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j]);
				}
				if(a[i][j - 1] == a[i][j]) {
					dp[i][j] = max(dp[i][j], dp[i][j - 1]);
				}
			}
		}
		cout << (dp[n][m] == 1 ? "Yes\n" : "No");
	}
	return 0;
}

